package no.ntnu.fp.services.impl;

import java.io.IOException;
import java.net.ConnectException;
import java.util.List;

import no.ntnu.fp.client.exceptions.NetworkException;
import no.ntnu.fp.messages.impl.AddOrUpdateAppointmentMessage;
import no.ntnu.fp.messages.impl.DeleteAppointmentMessage;
import no.ntnu.fp.messages.impl.AddOrUpdateAppointmentMessage.UpdateType;
import no.ntnu.fp.messages.impl.RequestAppointmentsForEmployeeMessage;
import no.ntnu.fp.model.Appointment;
import no.ntnu.fp.model.Employee;
import no.ntnu.fp.net.co.Connection;
import no.ntnu.fp.services.AppointmentService;

public class AppointmentServiceImpl extends EntityServiceImpl implements AppointmentService {

	public AppointmentServiceImpl(Connection connection) {
		super(connection);
	}

	@Override
	public void addAppointment(Appointment appointment) {
		AddOrUpdateAppointmentMessage message = 
			new AddOrUpdateAppointmentMessage(appointment, UpdateType.ADD);
		try {
			connection.send(message.toString());
		} catch (IOException e) {
			throw new NetworkException("Unable to send AddOrUpdateAppointmentMessage", e);
		}
	}
	
	@Override
	public void updateAppointment(Appointment appointment) {
		AddOrUpdateAppointmentMessage message = 
			new AddOrUpdateAppointmentMessage(appointment, UpdateType.UPDATE);
		try {
			connection.send(message.toString());
		} catch (IOException e) {
			throw new NetworkException("Unable to send AddOrUpdateAppointmentMessage", e);
		}
	}

	@Override
	public void deleteAppointment(Appointment appointment) {
		DeleteAppointmentMessage message = new DeleteAppointmentMessage(appointment);
		try {
			connection.send(message.toString());
		} catch (IOException ex) {
			throw new NetworkException("Unable to send DeleteAppointmentMessage", ex);
		}
	}

	@Override
	public void fetchAppointmentsForEmployee(Employee employee) {
		RequestAppointmentsForEmployeeMessage message 
						= new RequestAppointmentsForEmployeeMessage(employee);
		try {
			connection.send(message.toString());
		} catch (IOException ex) {
			throw new NetworkException(
					"Unable to send RequestAppointmentsForEmployeeMessage", ex);
		}
	}
}
